/*
 * **********************************************************************************************
 * Class: ServiceNode
 *  
 * @author Teresa Mafalda dos Reis Frazao
 * @version April 10, 2014 
 * 
 *  
 * <p> This class is a <code>Runnable</code> thread that is responsible for 
 * starting and stoping <code>ServiceNode</code>.
 * 
 * <p> Before <code>start</code> and <code>stop</code> operations, 
 * <code>ServiceManagerWorker</code> checks if these services are able to start and/or stop. 
 * **********************************************************************************************
 */

package com.feedzai.service.worker;

import com.feedzai.service.data.ServiceNode;
import com.feedzai.service.utils.ServiceManagerHelper;



/**
 * The Class ServiceManagerWorker.
 */
public class ServiceManagerWorker implements Runnable{
		
	/** The target service that I shall start or stop. */
	private ServiceNode myTargetService;
	
	/** The service manager helper. Used to check if I can perform operations in the service. */
	private ServiceManagerHelper serviceManagerHelper;
	
	/** The process that is <code>true</code> if I shall star the service. To stop a service this variable is <code>false</code>.  */
	private boolean process;
	
	
	/**
	 * Instantiates a new service manager worker.
	 *
	 * @param myTargetService the my target service that I shall start or stop. 
	 * @param serviceManagerHelper the service manager helper that allows to check if I can <code>start</code> or <code>stop</code> a service.
	 * @param process the switch that indicates if i shall <code>start</code> or <code>stop</code> a service. 
	 */
	public ServiceManagerWorker(ServiceNode myTargetService, ServiceManagerHelper serviceManagerHelper, boolean process) 
	{
		this.myTargetService = myTargetService; 
		this.serviceManagerHelper = serviceManagerHelper;
		this.process = process;
	}
	
	
	
	/*
	 * ---------------------------------------------------------------------------
	 * 							Public Interface 
	 * ---------------------------------------------------------------------------
	 */

	
	
	/** 
	 * Based on <code>process</code> value this thread checks if I shall start or stop the <code>myTargetService</code>.
	 */
	@Override
	public void run() {
		
		if(process)
		{
			startMyTargetService();
		}
		else
		{
			stopMyTargetService();
		}
	}
	
	
	
	/*
	 * ---------------------------------------------------------------------------
	 * 							Private Interface 
	 * ---------------------------------------------------------------------------
	 */
	
	
	/** 
	 * This methods allows <code>ServiceManagerWorker</code> start a <code>myTargetService</code> service.
	 * 
	 * To <code>start</code> a service I need to:
	 * 
	 * <p>1. Check if I have parent nodes.
	 * <p><p>1.1. If not I can start my service.
	 * <p><p>1.2. If I have parent services I need to check if they are all running.
	 * <p><p><p>1.2.1. If not, I cannot start my service. 
	 * <p><p><p>1.2.2. Otherwise I start my service. 
	 */
	private void startMyTargetService()
	{
		// 
		// Check if we are able to set the service status as start
		//
		if(this.serviceManagerHelper.canStart(this.myTargetService)) 
		{
			// Conditions are fulfilled so I can start my service
			this.serviceManagerHelper.startService(this.myTargetService);
		}
	}
	
	
	/** 
	 * This methods allows <code>ServiceManagerWorker</code> stop a <code>myTargetService</code> service.
	 * 
	 * To <code>stop</code> a service I need to:
	 * 
	 * <p>1. Check if I have child nodes.
	 * <p><p>1.1. If not I can stop my service.
	 * <p><p>1.2. If I have child services I need to check if they are all running.
	 * <p><p><p>1.2.1. If they are running, I cannot stop my service. 
	 * <p><p><p>1.2.2. Otherwise I can stop the service. 
	 */
	private void stopMyTargetService()
	{
		// 
		// We are able to set the service status as stop
		//
		if(this.serviceManagerHelper.canStop(this.myTargetService))
		{
			// Conditions are fulfilled so I can stop my service
			this.serviceManagerHelper.stopService(this.myTargetService);
		}
	}

}
